package mmc.ui;

import static mmc.resource.Text12p.glText12p;
import static org.lwjgl.input.Keyboard.*;
import static org.lwjgl.input.Mouse.getDWheel;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glColor4f;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import mmc.action.Action;
import mmc.resource.config.SavedVariables;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

public class KeyBindingWindow extends Window {
	private int item=1;
	private int row=1;
	private boolean changeState;
	private TreeMap<Action, int[]> sorted = new TreeMap<Action, int[]>();
	
	public KeyBindingWindow(float pos_x, float pos_y, float size_x, float size_y) {
		super(pos_x, pos_y, size_x, size_y);
		this.name = "KeyBinding";
		
		KeyBinding.addWindowShortcut(this.name, KEY_F11, this);		
	}
	
	public void init_SortedMap()
	{
		sorted.clear();
		for (Integer i : KeyBinding.binding.keySet()) 
		{
			if(sorted.containsKey(KeyBinding.binding.get(i)))
			{
				int[] x = sorted.get(KeyBinding.binding.get(i));
				if(x[0] != KEY_NONE)
					x[1] = i;
				else
					x[0] = i;
				sorted.put(KeyBinding.binding.get(i), x);
			}
			else
			{
				int[] x = new int[2];
				x[0] = i.intValue();
				sorted.put(KeyBinding.binding.get(i), x);
			}
		}		
	}

	@Override
	public void render() {
		if (!open) return;
		// TODO Auto-generated method stub
	
		int wheel = getDWheel();
		if(wheel != 0)
		{
			if(wheel > 0){
				item--;
				item = Math.max(item, 1);
			}
			else{
				item++;
				item = Math.min(item, sorted.size());
			}
		}
		
		glColor4f(1,1,1,0.2f);
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(0,0);
		GL11.glVertex2f(0,1);
		GL11.glVertex2f(1,1);
		GL11.glVertex2f(1,0);
		GL11.glEnd();
		
		
		
		glColor3f(0.54f, 0.25f, 0.07f); 		//Border
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x-0.001f, y-0.001f);
		GL11.glVertex2f(x-0.001f, y+height+0.001f);
		GL11.glVertex2f(x+width+0.001f, y+height+0.001f);
		GL11.glVertex2f(x+width+0.001f, y-0.001f);
		GL11.glEnd();
		
		glColor3f(1, 1, 1); 					//Window
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x, y);
		GL11.glVertex2f(x, y+height);
		GL11.glVertex2f(x+width, y+height);
		GL11.glVertex2f(x+width, y);
		GL11.glEnd();

		glColor3f(0, 0, 0);						//Middle tile
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x+width/2, y);
		GL11.glVertex2f(x+width/2, y+height);
		GL11.glVertex2f(x+width/2+0.001f, y+height);
		GL11.glVertex2f(x+width/2+0.001f, y);
		GL11.glEnd();
		
		glColor3f(0, 0, 0);						//Middle tile 2
		GL11.glBegin(GL11.GL_QUADS);
		GL11.glVertex2f(x+width*3/4, y);
		GL11.glVertex2f(x+width*3/4, y+height);
		GL11.glVertex2f(x+width*3/4+0.001f, y+height);
		GL11.glVertex2f(x+width*3/4+0.001f, y);
		GL11.glEnd();
		
		GL11.glBegin(GL11.GL_QUADS);			//Header tile
		GL11.glVertex2f(x, y+0.05f);
		GL11.glVertex2f(x, y+0.051f);
		GL11.glVertex2f(x+width, y+0.051f);
		GL11.glVertex2f(x+width, y+0.05f);
		GL11.glEnd();

		glText12p("Action", x, y);
		glText12p("1. Key", x+width/2+0.005f, y);
		glText12p("2. Key", x+width*3/4+0.005f, y);
		
		if(changeState)
			glColor3f(0,1,0);
		else
			glColor3f(1,0,0);
		int help_item = Math.min(item, 11);
		GL11.glBegin(GL11.GL_QUADS);			//Choice		
		GL11.glVertex2f(x+width*(3-row)/4, y+0.05f*help_item);
		GL11.glVertex2f(x+width*(4-row)/4, y+0.05f*help_item);
		GL11.glVertex2f(x+width*(4-row)/4, y+0.05f*(help_item+1));
		GL11.glVertex2f(x+width*(3-row)/4, y+0.05f*(help_item+1));
		GL11.glEnd();
		
		
		glColor3f(0,0,0);
		
		
		int start = (item-11);
		start = Math.max(start, 0);

		int k=1;								
		List<Action> keys = new ArrayList<Action>(sorted.keySet());
		Action h = null;
		for (Action a : sorted.keySet()) {		
			if(keys.indexOf(a) < start)
				continue;
			
			
			if(h != null && h.getClass().getSuperclass() != a.getClass().getSuperclass())
			{
				glColor3f(0,0,0);				
				GL11.glBegin(GL11.GL_LINES);			//Choice		
				GL11.glVertex2f(x, y+0.05f*k);
				GL11.glVertex2f(x+width, y+0.05f*k);
				GL11.glEnd();
			}
			
			glText12p(a.name, x, y+0.05f*k);			
			glText12p(Keyboard.getKeyName(sorted.get(a)[0]), x+width/2+0.005f, y+0.05f*k);		
			glText12p(Keyboard.getKeyName(sorted.get(a)[1]), x+width*3/4+0.005f, y+0.05f*k);
			k++;
			
			h = a;
			
			if(k > 11)
				break;
		}
	}
	
	public int getIndexOfKeySet(int key)
	{
		int index = 0;
		for (Integer i : KeyBinding.binding.keySet()) {
			if(i == key)
				return index;
			
			index++;
		}
		return index;
	}

	@Override
	public void forwardKey(int key, char ch)
	{
		if(changeState)
		{
			List<Action> keys = new ArrayList<Action>(sorted.keySet());
			KeyBinding.changeValueOnKey(sorted.get(keys.get(item-1))[1-row], key, keys.get(item-1));
			init_SortedMap();
			changeState = false;
		}
		else
		{
			switch(key)
			{			
				case KEY_UP:
					item--;
					item = Math.max(item, 1);
					break;
					
				case KEY_DOWN:
					item++;
					item = Math.min(item, sorted.size());
					break;
				case KEY_RETURN:
					changeState = !changeState;
					break;
				case KEY_RIGHT: case KEY_LEFT:
					row = (row+1)%2;
					break;
				default:
					break;
			}
		}
	}

	public boolean isOpen()
	{
		return open;
	}
	
	@Override
	public void openWindow()
	{
		super.openWindow();
		init_SortedMap();
	}
	
	@Override 
	public void safeChangesOnShutdown(){
		SavedVariables.saveConfig();
	}
}
